// src/stores/authStore.js
import { defineStore } from 'pinia'
import axios from 'axios'

export const useAuthStore = defineStore('auth', {
  state: () => ({
    user: null,
    token: null,
  }),
  actions: {
    async login(username, password) {
      try {
        // const response = await axios.post(
        //   'https://beta.randomapi.com/api/3141b5683af0edf576dabdb50ef1ff64?fmt=prettyraw&sole',
        //   { username, password },
        // )
        const response = await axios.post('http://localhost:9988/user/info', { username, password })

        console.info(response.data)
        if (response.data.code === '200') {
          const { clientId, uuid } = response.data.data
          this.user = clientId
          this.token = uuid
          console.log('登录成功:', this.user)
        } else {
          this.user = null
          this.token = null
          console.log('登录失败:', response.data.msg)
        }
      } catch (error) {
        console.error('登录失败:', error)
      }
    },
    logout() {
      this.user = null
      this.token = null
    },
  },
})
